home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
wstr.exe
/
WSTR.H
< prev
Wrap
Text File
|
1993-02-25
|
11KB
|
271 lines
#ifndef WStrIncluded
#define WStrIncluded
// copyright (c) 1992, 1993 by Paul Wheaton
// 1916 Brooks #205, Missoula, MT 59801
//
// voice phone: (406)543-7543
// modem phone: (406)543-1144 (2400N81)
// CompuServe: 72707,207
// Internet: 72707.207@CompuServe.com
#include <string.h>
#include <WMisc.h>
#ifdef MAJORBBS
#include <stdlib.h>
#endif
#define DefaultStringExtra 15
/* used internally by "String". Since most strings are very small yet
will have a little bit more added to them, this extra allocation should
save quite a bit of time. The only time it would be a hendrance is when
an array of strings is created */
class String;
class StackString;
class String40;
class String120;
class BaseString
{
protected:
char* P; // character string
int Len; // Length of string, excluding null character
int Alloc; // amount of actual storage allocated (including null char)
friend String;
friend StackString;
friend String40;
friend String120;
public:
operator const char*() const {return P;}
char& operator[](int Index);
Bool operator<(const BaseString& S) const { return strcmp(P, S.P) < 0; }
Bool operator>(const BaseString& S) const { return strcmp(P, S.P) > 0; }
Bool operator<=(const BaseString& S) const { return strcmp(P, S.P) <= 0; }
Bool operator>=(const BaseString& S) const { return strcmp(P, S.P) >= 0; }
Bool operator==(const BaseString& S) const;
Bool operator!=(const BaseString& S) const { return !(*this==S); }
Bool operator<(const char* CS) const { return strcmp(P,CS) < 0; }
Bool operator>(const char* CS) const { return strcmp(P,CS) > 0; }
Bool operator<=(const char* CS) const { return strcmp(P,CS) <= 0; }
Bool operator>=(const char* CS) const { return strcmp(P,CS) >= 0; }
Bool operator==(const char* CS) const { return strcmp(P,CS) == 0; }
Bool operator!=(const char* CS) const { return strcmp(P,CS) != 0; }
friend Bool operator<(const char* cs, const BaseString& S)
{ return strcmp(cs, (S.P)) < 0; }
friend Bool operator>(const char* cs, const BaseString& S)
{ return strcmp(cs, (S.P)) > 0; }
friend Bool operator<=(const char* cs, const BaseString& S)
{ return strcmp(cs, (S.P)) <= 0; }
friend Bool operator>=(const char* cs, const BaseString& S)
{ return strcmp(cs, (S.P)) >= 0; }
friend Bool operator==(const char* cs, const BaseString& S)
{ return strcmp(cs, (S.P)) == 0; }
friend Bool operator!=(const char* cs, const BaseString& S)
{ return strcmp(cs, (S.P)) != 0; }
int Length() const { return Len; }
void ToLower(); // force all of StackString to lower case
void ToUpper(); // force all of StackString to upper case
int Capacity() const { return Alloc - 1; }
int Size() const { return (Len+2); } // the current size to store
int Index(char SearchChar, int StartIndex=0) const;
int Index(const char* SearchStr, int StartIndex=0) const;
Bool Find(char SearchChar, int StartIndex=0) const
{return (Index(SearchChar,StartIndex)!=NotFound);}
Bool Find(const char* SearchStr, int StartIndex=0) const
{return (Index(SearchStr,StartIndex)!=NotFound);}
int Count(char C) const; // how many of this character occur
void Delete(int Index=0,int Length=1);
void DeleteLast();
void Trim(); // chop off leading and trailing spaces
void TrimLead();
void TrimTrail();
void TrimTrailTo(char C);
// remove last chars until C is found, then remove C too!
char At(int Index) const;
char operator()(int Index) const {return At(Index);}
char Last() const; // the last char in the string
#ifdef MAJORBBS
void* operator new(size_t size){return malloc(size);}
void operator delete(void* p) {free(p);}
#endif
};
class String: public BaseString
{
protected:
void ReNew(int NewCapacity);
void New(); // Uses Alloc
public:
String(const char& C, int L=1, int Extra=DefaultStringExtra);
// String S(' ',20) constructs a string consisting of 20 spaces
String(int Extra=DefaultStringExtra);
// constructor based on size desired or size not yet known
String(const char*, int Extra=DefaultStringExtra);
// a char* type string is used to construct a String type string
String(const BaseString&, int Extra=DefaultStringExtra);
// creating one String from another
#ifdef MAJORBBS
~String() {free(P);}
#else
~String() {delete P;}
#endif
// assignment operators
void operator=(const BaseString&);
void operator=(const char*);
void operator=(const char);
// String concatination (S1=S2+S3)
// String operator+(const StackString& S) const;
String operator+(const String40& S) const;
String operator+(const String120& S) const;
String operator+(const String&) const;
String operator+(const char*) const;
String operator+(char C) const;
// friend String operator+(const StackString&, const String&);
friend String operator+(const String40&, const String&);
friend String operator+(const String120&, const String&);
friend String operator+(const char* CS, const String& S);
friend String operator+(char C, const String& S);
// Appending stuff to a String
void operator+=(const BaseString&);
void operator+=(const char*);
void operator+=(char);
void Left(int NewSize); // resize string and left justify text
void Right(int NewSize);
void Center(int NewSize);
void Just(int Type, int NewSize);
// parses out to Left, Right or Center
void Tail(int NewSize); // works like "Right" cept left chars may be chopped
int ReAlloc(int NewCapacity);
String At(int Index, int Length) const;
String operator()(int Index, int Length) const {return At(Index,Length);}
char At(int Index) const {return BaseString::At(Index);}
char operator()(int Index) const {return BaseString::At(Index);}
String Before(int Index) const {return At(0,Index);}
String Through(int Index) const {return At(0,Index+1);}
String From(int Index) const {return At(Index,Len-Index);}
String After(int Index) const {return At(Index+1,Len-Index-1);}
void Insert(char C,int Index=0);
void Insert(const char* St,int Index=0);
};
class StackString: public BaseString
{
protected:
friend String;
public:
void operator=(const BaseString&);
void operator=(const char*);
void operator=(const char);
void operator+=(const BaseString&);
void operator+=(const char*);
void operator+=(char);
void Left(int NewSize); // resize string and left justify text
void Right(int NewSize);
void Center(int NewSize);
void Just(int Type, int NewSize);
void Tail(int NewSize); // works like "Right" cept left chars may be chopped
void Sub(StackString& D, int Index, int Length);
void Insert(char C,int Index=0);
void Insert(const char* St,int Index=0);
};
class String40: public StackString
{
protected:
char Buf[41];
friend String;
public:
String40();
String40(const char& C, int L=1);
String40(const char*);
String40(const BaseString&);
String40 operator+(const String40& S) const;
String40 operator+(const char* S) const;
String40 operator+(char C) const;
friend String40 operator+(const char* S1, const String40& S2);
friend String40 operator+(char C, const String40& S);
String40 At(int Index, int Length) const;
String40 operator()(int Index, int Length) const {return At(Index,Length);}
char At(int Index) const {return BaseString::At(Index);}
char op